package com.csw.shuanfa.CodeImprove.exercise.future;

import com.csw.shuanfa.utils.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author Fox
 * 电商询价业务
 */
@Slf4j
public class CompletionServiceDemo {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        //创建线程池
        ExecutorService threadPool = ThreadPoolUtil.getExecutorServiceCPU();

        try {
            //创建CompletionService
            CompletionService<Integer> cs = new ExecutorCompletionService<>(threadPool);
            //异步向电商S1询价
            cs.submit(() -> getPriceByS1());
            //异步向电商S2询价
            cs.submit(() -> getPriceByS2());
            //异步向电商S3询价
            cs.submit(() -> getPriceByS3());
            //将询价结果异步保存到数据库
            for (int i = 0; i < 3; i++) {
                //从阻塞队列获取futureTask
                Integer r = cs.take().get();
                threadPool.execute(() -> save(r));
            }
        } catch (Exception e) {
            threadPool.shutdown();
            throw new RuntimeException(e);
        }


    }

    private static void save(Integer r) {
        log.error("保存询价结果:{}", r);
    }

    private static Integer getPriceByS1() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(5000);
        log.error("电商S1询价信息1200");
        return 1200;
    }

    private static Integer getPriceByS2() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(8000);
        log.error("电商S2询价信息1000");
        return 1000;
    }

    private static Integer getPriceByS3() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(3000);
        log.error("电商S3询价信息800");
        return 800;
    }
}
